package com.fulei.logic;

import android.annotation.SuppressLint;

import com.bjfl.saleManager.util.LogUtil;
import com.dwin.navy.serialportapi.SerailPortOpt;

@SuppressLint({"SdCardPath"})
public class UpsOperate
{
  private static SerailPortOpt serialPort = null;
  public static String comNo = "";
  public static String baudRate = "";
  public static int AbnormalCount = 0;
  
  public static boolean OpenPort()
  {
    try
    {
      serialPort = new SerailPortOpt();
      
      serialPort.mDevNum = Integer.parseInt(comNo);
      serialPort.mSpeed = Integer.parseInt(baudRate);
      serialPort.mDataBits = 8;
      serialPort.mStopBits = 1;
      serialPort.mParity = 110;
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
    return true;
  }
  
  public static void closeSerialPort()
  {
    try
    {
      if (serialPort.mFd != null) {
        serialPort.closeDev(serialPort.mFd);
      }
      serialPort.mFd = null;
      serialPort = null;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
  }
  
  public static int upsCheckStatus()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 81, 49, 13 };
      
      LogUtil.WriteUPSInfo("upsCheckStatusSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.WriteUPSInfo(
        "upsCheckStatusRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData != null) && (ReadData.length > 37))
      {
        if (ReadData[38] == 49) {
          AbnormalCount += 1;
        } else {
          AbnormalCount = 0;
        }
        if (AbnormalCount >= 6)
        {
          AbnormalCount = 0;
          System.out.println("220V电源丢失超过一分钟未恢复,准备关机");
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.WriteUPSInfo("状态查询命令异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int closeUPS()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      String tSpan = "";
      byte[] timeSpan = tSpan.getBytes("UTF-8");
      byte[] SendData = new byte[9];
      
      SendData[0] = 83;
      SendData[1] = 48;
      SendData[2] = 50;
      SendData[3] = 82;
      
      System.arraycopy(timeSpan, 0, SendData, 4, timeSpan.length);
      SendData[8] = 13;
      
      LogUtil.WriteUPSInfo("closeUPSSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.WriteUPSInfo(
        "closeUPSRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
    }
    catch (Exception ex)
    {
      LogUtil.WriteUPSInfo("状态查询命令异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int closeUPSMain()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 83, 48, 50, 82, 48, 48, 48, 51, 13 };
      

      LogUtil.WriteUPSInfo("closeUPSMainSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.WriteUPSInfo(
        "closeUPSMainRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
    }
    catch (Exception ex)
    {
      LogUtil.WriteUPSInfo("状态查询命令异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static byte GetBCC(byte[] temp, int len)
  {
    byte A = 0;
    for (int i = 0; i < len; i++) {
      A = (byte)(A ^ temp[i]);
    }
    return A;
  }
}
